package com.fch.sdlibrary.controller;

import com.fch.sdlibrary.api.BaseResponse;
import com.fch.sdlibrary.entity.Book;
import com.fch.sdlibrary.entity.BookClazz;
import com.fch.sdlibrary.service.BookClazzService;
import com.fch.sdlibrary.service.BookService;
import com.fch.sdlibrary.service.UserService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.security.Principal;
import java.util.List;

/**
 * @program: sdlibrarySpring
 * @description: 图书控制类
 * @CreatedBy: fch
 * @create: 2022-05-04 19:50
 **/
@CrossOrigin
@RestController
public class BookController {
    private BaseResponse baseResponse;
    @Autowired
    private BookService bookService;
    @Autowired
    private UserService userService;
    @Autowired
    private BookClazzService bookClazzService;

    /**
     * @Author: fch
     * @Description: 查询所有图书
     * @DateTime: 20:33 6/5/2022
     * @Params: []
     * @Return: com.fch.sdlibrary.api.BaseResponse
     **/
    @GetMapping("QueryAllBooks")
    public BaseResponse queryAllBooks() {
        baseResponse = new BaseResponse();
        List<Book> books = bookService.queryAllBooks();
        baseResponse.setCode(200);
        if (books.isEmpty())
            baseResponse.setMsg("查询为空");
        else
            baseResponse.setMsg("查询到" + books.size() + "条记录");
        baseResponse.setData(books);
        return baseResponse;
    }

    /**
     * @Author: fch
     * @Description: 查询所有图书分类
     * @DateTime: 20:35 6/5/2022
     * @Params: []
     * @Return: com.fch.sdlibrary.api.BaseResponse
     **/
    @GetMapping("QueryAllBookClass")
    public BaseResponse queryAllBookClazz() {
        baseResponse = new BaseResponse();
        List<BookClazz> bookClazzes = bookService.queryAllBookClazz();
        baseResponse.setCode(200);
        if (bookClazzes.isEmpty())
            baseResponse.setMsg("图书分类查询为空");
        else
            baseResponse.setMsg("查询到" + bookClazzes.size() + "条记录");
        baseResponse.setData(bookClazzes);
        return baseResponse;
    }

    /**
     * @Author: fch
     * @Description: 分页查询所有图书
     * @DateTime: 13:21 8/5/2022
     * @Params: [pageNum, pageSize]
     * @Return: com.fch.sdlibrary.api.BaseResponse
     **/
    @GetMapping("QueryAllBookByPage")
    public BaseResponse queryAllBookByPage(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize", required = false, defaultValue = "6") Integer pageSize) {
        baseResponse = new BaseResponse();
        baseResponse.setCode(200);
        PageInfo<Book> bookPageInfo = bookService.queryAllBooksByPage(pageNum, pageSize);
        baseResponse.setData(bookPageInfo);
        return baseResponse;
    }

    /**
     * @Author: fch
     * @Description: 以图书id查询图书
     * @DateTime: 13:47 8/5/2022
     * @Params: [id]
     * @Return: com.fch.sdlibrary.api.BaseResponse
     **/
    @GetMapping("QueryBook")
    public BaseResponse queryBook(@RequestParam String id) {
        baseResponse = new BaseResponse();
        Book book = bookService.queryBookById(id);
        if (book != null)
            book.setBookClassName(bookClazzService.getBookClassName(book.getBookClassCode()));
        baseResponse.setCode(200);
        baseResponse.setData(book);

        return baseResponse;
    }

    @PostMapping("GetUserBook")
    public BaseResponse getUserBook(Principal principal) {
        baseResponse = new BaseResponse();
        Book book = userService.hasBorrow(principal.getName());
        baseResponse.setCode(200);
        baseResponse.setData(book);
        return baseResponse;
    }
}
